﻿using NfsBLL;
using NfsCommon.Util;
using NfsModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MobileWeb.Controllers
{
    [BaseController(IsCheck = false)]
    public class LoginController : Controller
    {
        //
        // GET: /Login/
        public ActionResult Login()
        {
            if (CookieHelper.GetValue("nfs_UserId") == null || CookieHelper.GetValue("nfs_UserId") == "")
            {
                return View();
            }
            else {
                return Redirect("/Home/Index"); 
            }
           
        }
        #region 登录
        [HttpPost]
        public JsonResult GetLogin(string userName, string userPwd)
        {
            try
            {
             AccountBLL bll = new AccountBLL();
            account model = bll.GetLogin (userName, userPwd);
            if (model == null)
            {
                return Json(new { MessageCode = "100" }, JsonRequestBehavior.AllowGet);
            }
            else
            {
                if (model.IsAdmin ==0)
                {

                    CookieHelper.Save("nfs_UserId", Encrypt.Encode(model.id.ToString()), 3);
                    int id = int.Parse(CookieHelper.GetDecodeCookie("nfs_UserId"));
                    AgencyBLL agbll = new AgencyBLL();
                    agency ag_model = agbll.GetModel_account(id);
                    if (ag_model.shState != 1)
                    {
                        return Json(new { MessageCode = "50" }, JsonRequestBehavior.AllowGet);//表示未审核
                    }
                    else
                    {
                        return Json(new { MessageCode = "0", model = model }, JsonRequestBehavior.AllowGet);//表示未审核

                    }
                }
                else
                {
                    return Json(new { MessageCode = "20" }, JsonRequestBehavior.AllowGet);//表示未审核
                
                }
              
            }
            }
            catch (Exception)
            {

                return Json(new { MessageCode = "200" }, JsonRequestBehavior.AllowGet);
            }
          
        }
        #endregion

        /// <summary>
        /// 退出时清楚Cookie
        /// </summary>
        /// <returns></returns>
        public ActionResult ClearSession()
        {

            CookieHelper.Remove("nfs_UserId");
            return RedirectToAction("Login","Login","");
        }

        public JsonResult OutLogin()
        {

            try
            {
                CookieHelper.Remove("nfs_UserId");
            return Json(new {MessageCode = "0" },JsonRequestBehavior.AllowGet);

            }
            catch (Exception)
            {
                
             return Json(new {MessageCode = "200" },JsonRequestBehavior.AllowGet);

            }
        
        }

    }
}
